/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codegen.glsl.ui;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.material.TechniqueDef;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.system.awt.AwtPanel;
import com.jme3.system.awt.AwtPanelsContext;
import com.jme3.system.awt.PaintMode;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 *
 * @author hungcuong
 */
public class MatPreviewApp extends SimpleApplication {

    private static MatPreviewApp app;
    private static AwtPanel panel;

    private Material mat;
    public static HashMap<String, String> map = new HashMap<String, String>();
    String vertexShader = "uniform mat4 g_WorldViewProjectionMatrix;\n"
            + "\n"
            + "attribute vec3 inPosition;\n"
            + "attribute vec2 inTexCoord;\n"
            + "\n"
            + "varying vec2 texCoord;\n"
            + "\n"
            + "void main(){\n"
            + "    gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);\n"
            + "    texCoord = inTexCoord;\n"
            + "}";
    String fragmentShader = "varying vec2 texCoord;\n"
            + "\n"
            + "uniform sampler2D m_ColorMap;\n"
            + "uniform vec4 m_Color;\n"
            + "\n"
            + "void main(){\n"
            + "    vec4 texColor = texture2D(m_ColorMap, texCoord);\n"
            + "    gl_FragColor = vec4(mix(m_Color.rgb, texColor.rgb, texColor.a), 1.0);\n"
            + "    gl_FragColor = vec4(1,1,1,1);"
            + "}";
    String vertLanguage = "GLSL100";
    String fragLanguage = "GLSL100";

    public MatPreviewApp() {
    }
    static HashMap<String,String> getMap() {
        return map;
    }
    public void startApp(final JPanel parent) {
        Logger.getLogger("com.jme3").setLevel(Level.WARNING);
        setShowSettings(false);
        AppSettings settings = new AppSettings(true);
        settings.setCustomRenderer(AwtPanelsContext.class);
        settings.setFrameRate(60);
        setSettings(settings);
        start();

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final AwtPanelsContext ctx = (AwtPanelsContext) getContext();
                panel = ctx.createPanel(PaintMode.Accelerated);
                panel.setPreferredSize(new Dimension(120, 120));
                ctx.setInputSource(panel);
                parent.add(panel);
            }
        });
    }

    @Override
    public void simpleInitApp() {
        flyCam.setDragToRotate(true);
        flyCam.setEnabled(false);

        Box b = new Box(Vector3f.ZERO, 1, 1, 1);
        Geometry geom = new Geometry("Box", b);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        //mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
        testShaderLoad();
        mat = makeNewMatDef();
        mat.selectTechnique("DemoTech", renderManager);
        geom.setMaterial(mat);
        rootNode.attachChild(geom);

        if (panel != null) {
            panel.attachTo(true, viewPort);
        }
    }

    public static void main(String[] args) {
        Logger.getLogger("com.jme3").setLevel(Level.WARNING);
        MatPreviewApp app = new MatPreviewApp();
        app.setShowSettings(false);
        AppSettings settings = new AppSettings(true);
        settings.setFrameRate(60);
        app.setSettings(settings);
        app.start();
    }

    public Material makeNewMatDef() {

        mat = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
        TechniqueDef technique = new TechniqueDef("DemoTech");
        technique.setShaderFile("__inmemory::vs.foo", "__inmemory::fs.foo", vertLanguage, fragLanguage);

        mat.getMaterialDef().addTechniqueDef(technique);

        return mat;

    }

    public void makeNewMat() {
    }

    public void testShaderLoad() {

        map.put("vs", vertexShader);
        map.put("fs", fragmentShader);

        assetManager.registerLocator("__inmemory::", InMemoryLocator.class);
        assetManager.registerLoader(TextLoader.class, "foo");

        //String newStr = (String) assetManager.loadAsset("__inmemory::vs.foo");

        //System.out.println(" " + newStr);
    }

    public void testShaderInjection() {
    }
}
